package com.hm.health.View;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hm.health.R;

import java.util.ArrayList;

/**
 * how to use
 *
 * 1. xml config
 * <?xml version="1.0" encoding="utf-8"?>
 * <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 *     xmlns:app="http://schemas.android.com/apk/res-auto"
 *     xmlns:tools="http://schemas.android.com/tools"
 *     android:layout_width="match_parent"
 *     android:layout_height="match_parent"
 *     android:orientation="vertical"
 *     tools:context="com.example.administrator.piechartview.MainActivity">
 *
 *
 *     <com.goodix.gh31x.widget.GHSleepPieChartView
 *         android:id="@+id/gh_sleep_pie_chart"
 *         android:layout_width="match_parent"
 *         android:layout_height="wrap_content" />
 *
 * </LinearLayout>
 *
 * 2. code
 *         GHSleepPieChartView pieChartView = findViewById(R.id.gh_sleep_pie_chart);
 *         pieChartView.updateSleepStatus(GHSleepPieChartView.SleepStatus.AWAKE, 70, "3小时4分");
 *         pieChartView.updateSleepStatus(GHSleepPieChartView.SleepStatus.SHALLOW_SLEEP, 100, "3小时4分");
 *         pieChartView.updateSleepStatus(GHSleepPieChartView.SleepStatus.DEEP_SLEEP, 100, "3小时4分");
 *         pieChartView.updateSleepStatus(GHSleepPieChartView.SleepStatus.FAST_EYE_MOVEMENT, 100, "3小时4分");
 */

public class SleepPieChartView extends LinearLayout {
    final static String LOG_TAG = "GHSleepPieChartView";

    private PieChartView mPieChart;

    private LinearLayout mContainer1;

    private LinearLayout mContainer2;

    private ArrayList<TextView> mSleepStatusViews;

    private ArrayList<TextView> mSleepDescriptionViews;

    private int[] mSleepColors;

    private String[] mSleepStatus;

    public enum  SleepStatus {
        DEEP_SLEEP, LIGHT_SLEEP, REM, AWAKE
    }

    public SleepPieChartView(Context context) {
        super(context);
    }

    public SleepPieChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_sleep_pie_chart, this);
        init();
    }

    private void init() {
        mPieChart = (PieChartView)findViewById(R.id.pie_chart);
        mPieChart.setCell(10);
        mPieChart.setInnerRadius(0.4f);

        int[] sleepStatusIDs = {R.id.tv_deep_sleep, R.id.tv_light_sleep, R.id.tv_rem_sleep, R.id.tv_awake};
        int[] sleepDescriptionIDs = {R.id.tv_deep_sleep_description, R.id.tv_light_sleep_description,
                R.id.tv_rem_sleep_description, R.id.tv_awake_description};
        int[] sleepColorIDs = {R.color.sleep_deep, R.color.sleep_light, R.color.sleep_rem, R.color.sleep_wake};
        mSleepStatus = getResources().getStringArray(R.array.sleep_status);

        mSleepStatusViews = new ArrayList<TextView>();
        mSleepDescriptionViews = new ArrayList<TextView>();
        mSleepColors = new int[sleepColorIDs.length];

        for(int i = 0; i < sleepStatusIDs.length; i++) {
            mSleepStatusViews.add((TextView) findViewById(sleepStatusIDs[i]));
            mSleepDescriptionViews.add((TextView)findViewById(sleepDescriptionIDs[i]));
            mSleepColors[i] = getResources().getColor(sleepColorIDs[i]);
        }
    }

    public void updateSleepStatus(SleepStatus status, int ratio, String description) {

        int index = -1;

        switch (status) {
            case DEEP_SLEEP:
                index = 0;
                break;
            case LIGHT_SLEEP:
                index = 1;
                break;
            case REM:
                index = 2;
                break;
            case AWAKE:
                index = 3;
                break;
            default:
                Log.e(LOG_TAG, "no such a sleep status");
                break;
        }

        if (index >= 0 && index <= mSleepStatusViews.size()) {
            mPieChart.addItemType(new PieChartView.ItemType(mSleepStatus[index], ratio, mSleepColors[index]));

            mSleepStatusViews.get(index).setText(mSleepStatus[index]);
            mSleepStatusViews.get(index).setVisibility(View.VISIBLE);

            mSleepDescriptionViews.get(index).setText(description);
            mSleepDescriptionViews.get(index).setVisibility(View.VISIBLE);
        }
    }

}
